<?php
namespace api\modules\v1\controllers;
use common\models\Balance;
use common\models\TransferLog;
use common\service\TransferService;

/*
	转帐控制器
*/
class TransferController extends CoreController
{

	/**
		转帐配置信息
		* b_type   账本类型
	*/
	public function actionConf()
	{
        $btype =  $this->request('b_type')??1;
		if(!$btype) $btype=1;

		$data = Balance::getBalance($this->_memberId, $btype);
		$data['tip'] = '转让前请确认接收方信息正确，转出后无法追回，请谨慎操作';

		$this->out('转帐配置信息', $data);
	}


	/*
		转帐操作
		from_type 来源账本类型
		money  金额
		to_member_id  接收人id
		password 密码
		to_type  可选
	*/
	public function actionIndexInfo()
	{

		if(md5($this->request('password')) != $this->_member['paypwd']) $this->error('密码错误');
		if(!$this->_member['state']) $this->error('你暂不能此操作');

		if(!$this->request('to_type')) $this->request['to_type'] = $this->request('from_type');
		if (!$this->request('to_member_id'))$this->error('请输入接受人信息');
		$res = TransferService::transfer($this->request, $this->_member);

		if(!$res['state']) $this->error($res['msg']);

		$this->out('操作成功');
	}


	/*
		转帐列表
	*/
	public function actionList()
	{
		$params = array(
			'page'	=> $this->request('page', 1),
			'limit' => $this->request('page_size', 15),
			'order' => 'id desc',
		);
		$whereArr = array();
		$whereArr['where'] = ['or', 'from_member_id='.$this->_memberId, 'to_member_id='.$this->_memberId];
		$data = TransferLog::dateList($whereArr, $params);

		$this->out('转帐列表', $data);
	}


}